//
//  UIConfigure.m
//  CedarFinancePH
//
//  Created by lshenrong on 2018/4/8.
//  Copyright © 2018年 cedarhd. All rights reserved.
//

#import "UIConfigure.h"
#import "MJRefreshNormalHeaderFormAdjustIphoneX.h"

@implementation UIConfigure

+ (void)configureUITextField:(UITextField *)textField fontSize:(CGFloat)size color:(UIColor *)color{
    NSString *str = textField.placeholder;
    if (IsEmptyStr(str)) {
        return;
    }
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    style.alignment = NSTextAlignmentLeft;
    UIFont *font = PingFangZhongChangGuiForFont(size);
    NSAttributedString *attri = [[NSAttributedString alloc] initWithString:str attributes:@{NSForegroundColorAttributeName:color,NSFontAttributeName:font, NSParagraphStyleAttributeName:style}];
    textField.attributedPlaceholder = attri;
}

+ (void)configureUITextField2:(UITextField *)textField fontSize:(CGFloat)size color:(UIColor *)color{
    NSString *str = textField.placeholder;
    if (IsEmptyStr(str)) {
        return;
    }
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    style.alignment = NSTextAlignmentLeft;
    UIFont *font = PingFangZhongChangGui_MediumForFont(size);
    NSAttributedString *attri = [[NSAttributedString alloc] initWithString:str attributes:@{NSForegroundColorAttributeName:color,NSFontAttributeName:font, NSParagraphStyleAttributeName:style}];
    textField.attributedPlaceholder = attri;
}


+ (void)configureUITextFieldPlaceholderCenter:(UITextField *)textField fontSize:(CGFloat)size color:(UIColor *)color{
    NSString *str = textField.placeholder;
    if (IsEmptyStr(str)) {
        return;
    }
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    style.alignment = NSTextAlignmentCenter;
    UIFont *font = PingFangZhongChangGuiForFont(size);
    NSAttributedString *attri = [[NSAttributedString alloc] initWithString:str attributes:@{NSForegroundColorAttributeName:color,NSFontAttributeName:font, NSParagraphStyleAttributeName:style}];
    textField.attributedPlaceholder = attri;
}



+ (void)configureUILabel:(UILabel*)label
                 keyWord:(NSString*)key
           hithlightFont:(UIFont*)font
          highlightColor:(UIColor*)color{
    if(!IsEmptyStr(label.text) && !IsEmptyStr(key)){
        NSMutableAttributedString *topicAttrString=nil;
        topicAttrString = [[NSMutableAttributedString alloc]initWithString:label.text];
        NSRange rang = [label.text rangeOfString:key];
        if (rang.location == NSNotFound) {
            return;
        }
        if(color){
            [topicAttrString addAttribute:NSForegroundColorAttributeName value:color range:rang];//设置 关键字 区域的文字颜色
            [topicAttrString addAttribute:NSBackgroundColorAttributeName value:[UIColor clearColor] range:rang];
        }
        if(font){
            [topicAttrString addAttribute:NSFontAttributeName value:font range:rang];
        }
        label.attributedText = topicAttrString;
    }
}

+ (void)configureUILabel:(UILabel*)label
                 range:(NSRange)range
           hithlightFont:(UIFont*)font
          highlightColor:(UIColor*)color{
    if(!IsEmptyStr(label.text) ){
        if (range.location == NSNotFound) {
            return;
        }
        NSMutableAttributedString *topicAttrString=nil;
        topicAttrString = [[NSMutableAttributedString alloc]initWithString:label.text];
     
        if(color){
            [topicAttrString addAttribute:NSForegroundColorAttributeName value:color range:range];//设置 关键字 区域的文字颜色
            [topicAttrString addAttribute:NSBackgroundColorAttributeName value:[UIColor clearColor] range:range];
        }
        if(font){
            [topicAttrString addAttribute:NSFontAttributeName value:font range:range];
        }
        label.attributedText = topicAttrString;
    }
}


+ (void)configureUILabel:(UILabel*)label
                   startRange:(NSRange)startRange
                   endRange:(NSRange)endRange
           hithlightFont:(UIFont*)font
          highlightColor:(UIColor*)color{
    if(!IsEmptyStr(label.text) ){
        NSMutableAttributedString *topicAttrString=nil;
        topicAttrString = [[NSMutableAttributedString alloc]initWithString:label.text];
        if(color){
            if (startRange.location != NSNotFound && startRange.length != NSNotFound) {
                [topicAttrString addAttribute:NSForegroundColorAttributeName value:color range:startRange];//设置 关键字 区域的文字颜色
                [topicAttrString addAttribute:NSBackgroundColorAttributeName value:[UIColor clearColor] range:startRange];
            }
            if (endRange.location != NSNotFound && endRange.length != NSNotFound) {
                [topicAttrString addAttribute:NSForegroundColorAttributeName value:color range:endRange];//设置 关键字 区域的文字颜色
                [topicAttrString addAttribute:NSBackgroundColorAttributeName value:[UIColor clearColor] range:endRange];
                
            }

          }
        if(font){
            if (startRange.location != NSNotFound && startRange.length != NSNotFound) {
                [topicAttrString addAttribute:NSFontAttributeName value:font range:startRange];
            }
            if (endRange.location != NSNotFound && endRange.length != NSNotFound) {
                [topicAttrString addAttribute:NSFontAttributeName value:font range:endRange];
            }
        }
        label.attributedText = topicAttrString;
    }
}

+ (void)configRefreshInTableView:(UITableView *)tableView target:(id)target action:(SEL)action {
    
//    NSMutableArray *mjRefreshingImages = [NSMutableArray array];
//    for (int i = 1; i <= 14; i++) {
//
//        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"small_refresh_picture_%d", i]];
//        [mjRefreshingImages addObject:image];
//    }
//
//    NSMutableArray *mjPullingImages = [NSMutableArray array];
//    UIImage *mjPullingImage = [UIImage imageNamed:[NSString stringWithFormat:@"small_refresh_picture_14"]];
//    [mjPullingImages addObject:mjPullingImage];
//
//    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingTarget:target refreshingAction:action];
//    [header setImages:mjRefreshingImages   forState:MJRefreshStateRefreshing];
//    [header setImages:mjRefreshingImages   forState:MJRefreshStateIdle];
//    [header setImages:mjPullingImages      forState:MJRefreshStatePulling];
//
//    header.lastUpdatedTimeLabel.hidden  = YES;
//    header.automaticallyChangeAlpha     = NO;
//    tableView.mj_header                 = header;
    
    // 下拉刷新
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:target refreshingAction:action];
    header.automaticallyChangeAlpha = YES;
    header.lastUpdatedTimeLabel.hidden = YES;
    [header setTitle:@"下拉刷新" forState:MJRefreshStateIdle];
    [header setTitle:@"松手更新" forState:MJRefreshStatePulling];
    [header setTitle:@"正在刷新" forState:MJRefreshStateRefreshing];
    header.stateLabel.textColor = [UIColor colorWithRGB:0x999999];
    // 设置图片
    UIImageView *imageView = [header valueForKeyPath:@"arrowView"];
    imageView.image = [UIImage imageNamed:@"RefreshArrow"];
    tableView.mj_header = header;
}

+ (void)configRefreshInFulScreenTableView:(UITableView *)tableView target:(id)target action:(SEL)action {
    MJRefreshNormalHeader *header = nil;
    // 下拉刷新
    if (IsPhoneX) {
        header =  (MJRefreshNormalHeader *)[MJRefreshNormalHeaderFormAdjustIphoneX headerWithRefreshingTarget:target refreshingAction:action];
    }else{
        header = [MJRefreshNormalHeader headerWithRefreshingTarget:target refreshingAction:action];
    }
    header.automaticallyChangeAlpha = YES;
    header.lastUpdatedTimeLabel.hidden = YES;
    [header setTitle:@"下拉刷新" forState:MJRefreshStateIdle];
    [header setTitle:@"松手更新" forState:MJRefreshStatePulling];
    [header setTitle:@"正在刷新" forState:MJRefreshStateRefreshing];
    header.stateLabel.textColor = [UIColor colorWithRGB:0x999999];
    // 设置图片
    UIImageView *imageView = [header valueForKeyPath:@"arrowView"];
    imageView.image = [UIImage imageNamed:@"RefreshArrow"];
    tableView.mj_header = header;
}


+ (void)configLoadMoreInTableView:(UITableView *)tableView  target:(id)target action:(SEL)action {
    // 上拉加载
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:target refreshingAction:action];
    // 禁止自动加载
    footer.automaticallyRefresh = YES;
    //    footer.userInteractionEnabled = NO;
    // 设置文字
    [footer setTitle:@"" forState:MJRefreshStateIdle];
    [footer setTitle:@"加载中..." forState:MJRefreshStateRefreshing];
    [footer setTitle:@"" forState:MJRefreshStateNoMoreData];
    footer.stateLabel.textColor =  [UIColor colorWithRGB:0x999999];
    tableView.mj_footer = footer;
}

#pragma mark - UIBarButtonItem

+ (UIBarButtonItem *)barButtonItemWithTitle:(NSString *)titlte target:(id)target action:(SEL)action{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:titlte forState:UIControlStateNormal];
    [button setTitleColor:kMasterColor forState:UIControlStateNormal];
    [button setTitleColor:kTextHighlightColor forState:UIControlStateHighlighted];
    button.titleLabel.font = PingFangZhongChangGuiForFont(15);
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    [button sizeToFit];
    UIBarButtonItem *item =  [[UIBarButtonItem alloc]initWithCustomView:button];
    return item;
}

+ (UIBarButtonItem *)barButtonItemWithTitle:(NSString *)titlte color:(UIColor *)color  target:(id)target action:(SEL)action{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:titlte forState:UIControlStateNormal];
    [button setTitleColor:color forState:UIControlStateNormal];
    [button setTitleColor:kTextHighlightColor forState:UIControlStateHighlighted];
    button.titleLabel.font = PingFangZhongChangGuiForFont(15);
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    [button sizeToFit];
    UIBarButtonItem *item =  [[UIBarButtonItem alloc]initWithCustomView:button];
    return item;
}





+ (UIBarButtonItem *)barButtonItemCloseWithTarget:(id)target action:(SEL)action{
    UIButton *closeBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
    [closeBtn setImage:[UIImage imageNamed:@"Close"] forState:UIControlStateNormal];
    [closeBtn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *item =  [[UIBarButtonItem alloc]initWithCustomView:closeBtn];
    return item;
}


+ (UIBarButtonItem *)barWhiteButtonItemCloseWithTarget:(id)target action:(SEL)action{
    UIButton *closeBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 23, 40)];
    [closeBtn setImage:[UIImage imageNamed:@"BackWhite"] forState:UIControlStateNormal];
    [closeBtn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *item =  [[UIBarButtonItem alloc]initWithCustomView:closeBtn];
    return item;
}

+ (UITableView *)tableViewFromFactory{
    UITableView *tableView = [[UITableView alloc]init];
    tableView.showsVerticalScrollIndicator = NO;
    tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    tableView.tableFooterView = [[UIView alloc] init];
    //if (@available(iOS 11.0, *))
    {//解决mj 上拉加载滑动问题
        tableView.estimatedRowHeight = 0;
        tableView.sectionHeaderHeight = 0;
        tableView.sectionFooterHeight = 0;
    }
    tableView.backgroundColor = kAppBackgoundColor;
    [tableView setSeparatorColor:kSeparateLineColor];
    return tableView;
}

+ (UITableView *)tableViewStyleGroupedFromFactory{
    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    tableView.showsVerticalScrollIndicator = NO;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.tableFooterView = [[UIView alloc] init];
    //if (@available(iOS 11.0, *))
    {//解决mj 上拉加载滑动问题
        tableView.estimatedRowHeight = 0;
        tableView.sectionHeaderHeight = 0;
        tableView.sectionFooterHeight = 0;
    }
    tableView.backgroundColor = kAppBackgoundColor;
    return tableView;
}

#pragma mark - font

+ (UIFont *)configureAPPFont:(CGFloat)fontSize{
    UIFont *font =  [UIFont fontWithName:@"PingFangSC-Regular" size:fontSize];
    if (!font) {
        font = [UIFont systemFontOfSize:fontSize];
    }
    return font;
}

+ (UIFont *)configureAPPMediumFont:(CGFloat)fontSize{
    UIFont *font =  [UIFont fontWithName:@"PingFangSC-Medium" size:fontSize];
    if (!font) {
        font = [UIFont systemFontOfSize:fontSize];
    }
    return font;
}


+ (JSBadgeView *)redPointView:(UIView *)parentView positon:(JSBadgeViewAlignment)alignment{
    JSBadgeView *redHotBadgeView = [[JSBadgeView alloc]initWithParentView:parentView alignment:alignment];
    //redHotBadgeView.badgePositionAdjustment = CGPointMake(0, 5);
    redHotBadgeView.badgeText = nil;
    redHotBadgeView.onlySmallRedPoint = YES;
    //当更新数字时，最好刷新，不然由于frame固定的，数字为2位时，红圈变形
    // [_jsBadgeView setNeedsLayout];
    redHotBadgeView.badgeBackgroundColor = [UIColor redColor];
    redHotBadgeView.badgeOverlayColor = [UIColor clearColor];//没有反光面
    redHotBadgeView.badgeStrokeColor = [UIColor redColor];//外圈的颜色，默认是白色
    redHotBadgeView.badgeShadowColor = [UIColor clearColor];//没有阴影
    return redHotBadgeView;
}

+ (UIBarButtonItem *)barButtonItemWithTitle:(NSString *)titlte
                                      color:(UIColor *)color
                             highLightColor:(UIColor *)hColor
                                     target:(id)target
                                     action:(SEL)action{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:titlte forState:UIControlStateNormal];
    [button setTitleColor:color forState:UIControlStateNormal];
    [button setTitleColor:hColor forState:UIControlStateHighlighted];
    button.titleLabel.font = PingFangZhongChangGuiForFont(15);
    [button sizeToFit];
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *item =  [[UIBarButtonItem alloc]initWithCustomView:button];
    return item;
}

+ (UIBarButtonItem *)barButtonItemWithImageTitle:(NSString *)titlte
                                     target:(id)target
                                     action:(SEL)action{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:titlte] forState:UIControlStateNormal];
    [button sizeToFit];
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *item =  [[UIBarButtonItem alloc]initWithCustomView:button];
    return item;
}




@end
